/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package bpmlab.kivulifx.controller;

import bpmlab.kivulifx.application.Context;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.CheckBox;
import javafx.scene.control.TextField;
import javafx.stage.DirectoryChooser;
import javafx.stage.Stage;

/**
 * FXML Controller class
 *
 * @author bpmlab
 */
public class WorkspaceController implements Initializable {

    @FXML
    private Button search;
    @FXML
    private TextField path;
    @FXML
    private CheckBox defaultDirectory;

    private Stage dialog;
    private File file;

    @Override
    public void initialize(URL url, ResourceBundle rb) {
        String pathFile = System.getProperty("user.home") + File.separator + "KivuliProjects";
        file = new File(pathFile);
        path.setText(pathFile);
    }

    @FXML
    public void searchAction(ActionEvent event) {
        DirectoryChooser chooser = new DirectoryChooser();
        file = chooser.showDialog(null);
        if (file != null) {
            path.setText(file.getPath());
        }
    }

    @FXML
    public void closeAction(ActionEvent event) {
        System.exit(0);
    }

    @FXML
    public void okAction(ActionEvent event) {
        if (file != null) {
            File f = new File(file.getPath());
            if (!f.exists()) {
                f.mkdir();
            }
            Context.getCurrentInstance().setWorkspace(file.getPath());
            dialog.close();
        }
    }

    public void setDialog(Stage dialog) {
        this.dialog = dialog;
    }

}
